'use strict';

const Controller = require('egg').Controller;

// 定义创建接口的请求参数规则
const createRule = {
  name: {type:'string', required:true,msg:'用户名必填'},
  nickname: 'string',
  age:{ type:'integer', required:true, message:'年龄必须',desc:'年龄必填' },
  mobile: { type:'string' },
};

class UsersController extends Controller {

  // 基本查询
  async index() {
    console.log('geta')
    let { ctx } = this;
    ctx.body = {
        code:1,
        data:{
            name:'成功'
        }
    }
  }

  async show() {
    console.log('s')
  }
   
  async create() {
    const { ctx, service } = this;
    let { name, age, mobile, nickname, sex } = ctx.request.body;
    await ctx.validate(createRule,ctx.request.body);
    await ctx.validate({ mobile: { type:'tel' } });
    await ctx.validate({ nickname: { type:'name' } });
    let ret = await service.users.create({ name, age, nickname, mobile, sex });
    // ctx.logger.info('nsnsnsnsnnsns','sssw')
    ctx.body = {
      code:1,
      message:'success',
      data:ret
    }
  }

  async update(){
    console.log('update')
  }

  async destroy() {
    console.log('destory')
  }

}

module.exports = UsersController;
